#include "typedfs.h"
#include "img_tool.h"

/***********  FOR MORPHOLOGY ***********/

/** R1 := R1 or R2 */
void OrR(void *R1, const void *R2, unsigned maxX)
{
  while(maxX>=4)
  {
    *(uint32_t*)R1 |= *(uint32_t*)R2;
    R1 = (uint32_t*)R1 + 1;
    R2 = (uint32_t*)R2 + 1;
    maxX -= 4;
  }
  while(maxX>=1)
  {
    *(int8_t*)R1 |= *(int8_t*)R2;
    R1 = (int8_t*)R1 + 1;
    R2 = (int8_t*)R2 + 1;
    maxX--;
  }
}


/** R1 := R1 and R2 */
void AndR(void *R1, const void *R2, unsigned maxX)
{
  while(maxX>=4)
  {
    *(uint32_t*)R1 &= *(uint32_t*)R2;
    R1 = (uint32_t*)R1 + 1;
    R2 = (uint32_t*)R2 + 1;
    maxX -= 4;
  }
  while(maxX>=1)
  {
    *(int8_t*)R1 &= *(int8_t*)R2;
    R1 = (int8_t*)R1 + 1;
    R2 = (int8_t*)R2 + 1;
    maxX--;
  }
}


/***********  FOR MASK FILTER ***********/


void AddLu32u8(unsigned Size, uint32_t *Accu, uint8_t *Add)
{
 while(Size--)
 {
   *Accu++ += *Add++;
 }
}


void SubLu32u8(unsigned Size, uint32_t *Accu, uint8_t *Sub)
{
 while(Size--)
 {
   *Accu++ -= *Sub++;
 }
}


/***********  FOR MEDIAN FILTER ***********/

